package com.leetcode.partition11;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/12/26 10:49
 */
public class LC1078Bigram分词 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String text = reader.readLine(), first = reader.readLine(), second = reader.readLine();
        for (String item : findOcurrences(text, first, second)) {
            writer.write(item + ", ");
        }
        writer.flush();
    }

    private static String[] findOcurrences(String text, String first, String second) {
        String[] arr = text.split("\\s+");
        List<String> answer = new ArrayList<>();
        for (int i = 0; i + 2 < arr.length; i++) {
            if (arr[i].equals(first) && arr[i + 1].equals(second)) answer.add(arr[i + 2]);
        }
        return answer.toArray(new String[answer.size()]);
    }
}
